import React, { Component } from 'react'

const Pubsub = require('pubsub-js')

export default class List extends Component {
  state = {
    isFirst: true,
    isLoading: false,
    err: '',
    list: []
  }

  updateState = (msg, data) => {
    this.setState(data)
  }

  componentDidMount = () => {
    Pubsub.subscribe('Update', this.updateState)
  }

  render() {
    const { isFirst, isLoading, err, list } = this.state
    return (
      <div className="list">
        {
          isFirst ? <h2>输入关键词，然后点击搜索按钮</h2> :
          isLoading ? <h2>查询中，请稍后....</h2> :
          err ? <h2>{err}</h2> :
          list.map(item => {
            return (
              <div key={ item.id }>
                <div>{ item.sName }</div>
                <div>{ item.age }</div>
              </div>
            )
          })
        }
      </div>
    )
  }
}
